package com.yc.bbs.controller;

import com.yc.bbs.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorAction {
    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl", params="action=config")
    public Map<String,Object> ueditorConfig(){
        Map<String,Object> ret = new HashMap<>();
        ret.put("imageActionName","uploadImage");
        ret.put("imageFieldName","file");
        ret.put("imageUrlPrefix","");
        return ret;
    }

    @Autowired
    UserController userController;
    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl", params="action=uploadImage")
    public Map<String,Object> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String,Object> ret = new HashMap<>();
        Result result = userController.uploadHead(file);
        ret.put("state","SUCCESS");
        String webPath = result.getData().toString();
        ret.put("url",webPath);
        ret.put("original",file.getOriginalFilename());
        ret.put("title",webPath.substring(webPath.lastIndexOf("/")+1));
        return ret;
    }
}